package com.czd.support.job;

import com.czd.support.common.Configuration;
import com.czd.support.service.RoomService;
import com.czd.support.util.HttpClientUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014-10-12 10:17)
 * Description:定期维护房间播放状态.
 */
@Component
public class SyncRoomIsPlay {

    @Autowired
    private RoomService roomService;
    private static final Log log = LogFactory.getLog(SyncRoomIsPlay.class);

    /**
     * 定期维护房间在线人数
     */
    @Scheduled(cron="0 0/15 7-23 * * ?")
    public void updateRoomPlayStatus(){
        log.info("开始获取房间播放状态........."+Configuration.getInstance().getSync_room_isplay_url());
        String roomStatusInfo = HttpClientUtils.doGet(Configuration.getInstance().getSync_room_isplay_url(),null);
        if (StringUtils.isBlank(roomStatusInfo)) {
            log.info("暂无房间需要更新状态.......");
            return;
        }
        JSONArray jsonArray = JSONArray.fromObject(roomStatusInfo);
        for (Object jsonArray1 :jsonArray){
            JSONObject jsonObject = (JSONObject)jsonArray1;
            int roomId = jsonObject.getInt("roomId");
            if( roomService.updatePlay(roomId,roomService.isPlaying(roomId,JSONArray.fromObject(jsonObject.get("userList"))))){
                log.error("更新成功===========================");
            }else{
                log.error("更新失败===========================");
            }
        }
    }
}
